/****************************************************
[	Author:		Michael Mozdzierz
[	Date:		01/30/2014
[	File:		Math.cpp
[	Course:		SGD
[
****************************************************/

#include "stdafx.h"
#include "Math.h"
#include <stdlib.h>
#include <assert.h>

const float Math::PI = 3.14159265359f;

float Math::to_degrees(float radians)
{
	return radians * (180.0f / PI);
}

float Math::to_radians(float degrees)
{
	return degrees * (PI / 180.0f);
}

float Math::distance(float a, float b)
{
	return max(a, b) - min(a, b);
}

float Math::lerp(float low, float high, float weight)
{
	return low + (high - low) * weight;
}

bool Math::RectContains(void* rect, void* point)
{
	float rectData[4];
	memcpy(rectData, rect, 16);
	float left = rectData[0],
		top = rectData[1],
		right = rectData[2],
		bottom = rectData[3];

	float pointData[2];
	memcpy(pointData, point, 8);
	float x = pointData[0],
		y = pointData[1];

	if (x < right && x > left &&
		y < bottom && y > top)
		return true;

	return false;
}

float Math::WrapAngle(float radians)
{
	const int two_pi = 621318;
	const int pi = 314159;
	int i = int(radians * 100000.f);

	int rad_wrapped = i % two_pi - pi;

	return rad_wrapped / 100000.f;
}

int Math::Wrap(int value, int min, int max)
{
	int range = max - min;

	assert(range != 0 && "min and max cannot be the same!");

	return (value - min) % range + min;
}